﻿/*
 * Copyright (C) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using UnityEngine;
using System.Collections;

public class PlayerAimController : MonoBehaviour {
    public GameObject AimPivotObject;

    // constants
    private const float MaxPanSpeed = 2.0f;
    private const float DemoPanSpeed = 5.0f;
    private const float MaxElevSpeed = 2.0f;
    private const float ElevMin = -15.0f;
    private const float ElevMax = 22.0f;
    private const float TouchFactorX = 150.0f;
    private const float TouchFactorY = 60.0f;

    // original position and rotation of some objects
    private Vector3 aimOrigPos;
    private Quaternion aimOrigRot;
    private Quaternion origRot;

    // demo rotation (for main menu or game over modes)
    private float demoAzimuth = 0.0f;

    // aim: azimuth and elevation
    private float azimuth = 0.0f;
    private float elevation = 0.0f;
    public float Azimuth { get { return azimuth; } }
    public float Elevation { get { return elevation; } }
    public float NormalizedElevation { get { return elevation - ElevMin; } }

    void Start () {
        origRot = gameObject.transform.rotation;
        aimOrigPos = AimPivotObject.transform.localPosition;
        aimOrigRot = AimPivotObject.transform.localRotation;
    }

    // Update is called once per frame
    void Update () {
        float joyX = Input.GetAxis("Horizontal");
        float joyY = Input.GetAxis("Vertical");

        if (Input.touchCount > 0) {
            Touch t = Input.touches[0];
            if (t.phase == TouchPhase.Moved) {
                joyX = TouchFactorX * t.deltaPosition.x / Screen.width;
                joyY = TouchFactorY * t.deltaPosition.y / Screen.height;
            }
        }

        bool real = GameManager.Instance.Mode == GameManager.GameMode.Playing;
        float aziToUse;

        if (real) {
            azimuth += joyX * MaxPanSpeed;
            azimuth %= 360.0f;
            if (azimuth < 0.0f) azimuth += 360.0f;
            elevation += joyY * MaxElevSpeed;
            if (elevation < ElevMin) elevation = ElevMin;
            if (elevation > ElevMax) elevation = ElevMax;
            demoAzimuth = azimuth;
            aziToUse = azimuth;
        }
        else {
            // demo mode
            demoAzimuth += DemoPanSpeed * Time.deltaTime;
            demoAzimuth %= 360.0f;
            aziToUse = demoAzimuth;
        }

        gameObject.transform.rotation = origRot;
        gameObject.transform.Rotate(0.0f, aziToUse, 0.0f);

        AimPivotObject.transform.localPosition = aimOrigPos;
        AimPivotObject.transform.localRotation = aimOrigRot;
        AimPivotObject.transform.Rotate(-elevation, 0, 0);

        Utils.EnableRenderers(AimPivotObject, real);
    }
}
